import { ShaohuoOrdersService } from 'services/Api'
import { DateFormat } from 'services/Util'

/* 搜索相关
-------------------------- */
export const ordersSearchMixin = {
  data () {
    return {
      search: '',
      district: '',
      orderType: '',
      orderStatus: '',
      isShipping: '',
      orderStime: '',
      orderEtime: '',
      preSendStime: '',
      preSendEtime: '',
      receiveStime: '',
      receiveEtime: '',
      sendStime: '',
      sendEtime: ''
    }
  },
  methods: {
    // 搜索条件改变
    searchCondChange (list) {
      this.search = ''
      this.district = ''
      this.orderType = ''
      this.orderStatus = ''
      this.isShipping = ''
      this.orderStime = ''
      this.orderEtime = ''
      this.preSendStime = ''
      this.preSendEtime = ''
      this.receiveStime = ''
      this.receiveEtime = ''
      this.sendStime = ''
      this.sendEtime = ''
      list.forEach(i => {
        switch (i.name) {
          case '模糊查询':
            this.search = i.value
            break
          case '捎货类型':
            this.orderType = i.value
            break
          case '订单归属地(区)':
            this.district = i.value
            break
          case '订单状态':
            this.orderStatus = i.value
            break
          case '是否指派捎货人':
            this.isShipping = i.value
            break
          case '下单时间':
            this.orderStime = DateFormat(i.value[0], 'yyyy-MM-dd hh:mm:ss') || ''
            this.orderEtime = DateFormat(i.value[1], 'yyyy-MM-dd hh:mm:ss') || ''
            break
          case '预计送达时间':
            this.preSendStime = DateFormat(i.value[0], 'yyyy-MM-dd hh:mm:ss') || ''
            this.preSendEtime = DateFormat(i.value[1], 'yyyy-MM-dd hh:mm:ss') || ''
            break
          case '接单时间':
            this.receiveStime = DateFormat(i.value[0], 'yyyy-MM-dd hh:mm:ss') || ''
            this.receiveEtime = DateFormat(i.value[1], 'yyyy-MM-dd hh:mm:ss') || ''
            break
          case '送达时间':
            this.sendStime = DateFormat(i.value[0], 'yyyy-MM-dd hh:mm:ss') || ''
            this.sendEtime = DateFormat(i.value[1], 'yyyy-MM-dd hh:mm:ss') || ''
            break
        }
      })
      this.getTableData()
    }
  }
}

/* 删除相关
-------------------------- */
export const ordersDeleteMixin = {
  data () {
    return {
      deleteText: '取消',
      deleteBtnText: '取消订单'
    }
  },
  computed: {
    // 删除参数
    cancelOrderParam () {
      return {
        acc_id: this.acc_id,
        uid: this.uid,
        order_id: this.selectIds
      }
    }
  },
  methods: {
    // 删除接口
    currentDeleteApi () {
      return ShaohuoOrdersService.cancelOrder(this.cancelOrderParam)
    }
  }
}

/* 指派相关
-------------------------- */
export const ordersAssignedMixin = {
  data () {
    return {
      assigned: '', // 指派捎货人
      assignedIndex: '',
      currentAssigned: '', // 当前指派操作
      assignedPopover: false,
      assignedList: [],
      hasAssignedList: false
    }
  },
  computed: {
    // 修改指派参数
    assignedParam () {
      return {
        acc_id: this.acc_id,
        list_id: this.selectIds,
        us_id: this.assigned
      }
    },
    shaohuoUserListParam () {
      return { acc_id: this.acc_id }
    }
  },
  methods: {
    // 显示下拉框 boo === true
    assignedVisibleShow (boo) {
      if (boo && !this.hasAssignedList) {
        this.shaohuoUserList()
      }
    },
    // 用户选择完毕
    assignedChange () {
      this.assignedPopover = true
      typeof this.assignedIndex === 'number'
        ? this.AsyncLoading('指派捎货人', this.assignedIndex, this.assignedApi)
        : this.AsyncLoading('指派捎货人', this.assignedApi)
    },
    // 指派单个
    assignedOne (id, index) {
      this.selectIds = id
      this.assignedIndex = index
    },
    // 批量指派已选的数据(idKey: 行数据中id对应的字段名)
    assignedSelected (idKey) {
      if (!this.selectList.length) return this.$message.warning('选择数据后才能操作')
      this.selectIds = this.selectList.map(item => item[idKey]).join(',')
      this.assignedIndex = ''
    },
    // 恢复自由接单
    assignedFreeSelected (idKey) {
      if (!this.selectList.length) return this.$message.warning('选择数据后才能操作')
      this.selectIds = this.selectList.map(item => item[idKey]).join(',')
      this.assigned = '0'
      this.AsyncLoading('恢复自由接单', this.assignedApi)
    },
    // 改变指派接口
    currentAssignedApi () {
      return this.assigned === '0' ? ShaohuoOrdersService.updateShipping(this.assignedParam) : ShaohuoOrdersService.modiShippin(this.assignedParam)
    },
    // 接口
    async assignedApi () {
      const [ err ] = await this.currentAssignedApi()
      this.apply = ''
      if (err) return this.$message.error(err.message || '操作失败')
      this.$message.success(`操作成功`)
      this.getTableData()
    },
    // 获取捎货人列表
    async shaohuoUserList () {
      const [ err, res ] = await ShaohuoOrdersService.shaohuoUserList(this.shaohuoUserListParam)
      if (err) return this.$message.error(err.message || '获取捎货人列表失败')
      this.assignedList = res.list
      this.hasAssignedList = true
    }
  }
}

/* 导出相关
-------------------------- */
export const ordersExportMixin = {
  computed: {
    // 导出参数
    exportShaohuoOrderParam () {
      return {
        acc_id: this.acc_id,
        page: this.page,
        page_size: this.pageSize,
        sort_field: this.sort_field,
        sort_type: this.sort_type,
        search: this.search,
        district: this.district,
        orderType: this.orderType,
        orderStatus: this.orderStatus,
        isShipping: this.isShipping,
        orderStime: this.orderStime,
        orderEtime: this.orderEtime,
        preSendStime: this.preSendStime,
        preSendEtime: this.preSendEtime,
        receiveStime: this.receiveStime,
        receiveEtime: this.receiveEtime,
        sendStime: this.sendStime,
        sendEtime: this.sendEtime
      }
    }
  },
  methods: {
    // 导出接口
    currentExportApi () {
      return ShaohuoOrdersService.exportShaohuoOrder(this.exportShaohuoOrderParam)
    }
  }
}
